package com.xw.base.config;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Configuration
public class WebAppConfigAdapter implements WebMvcConfigurer {

    @Resource
    private LoginAuthInterceptor loginAuthInterceptor;

    @Resource
    private CommonInterceptor commonInterceptor;

    /*********************************覆盖默认****************************************/

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("add interceptors");
        //登录拦截
        addLoginAuthInterceptor(registry);

        addCommonInterceptor(registry);
    }


    private void addLoginAuthInterceptor(InterceptorRegistry registry) {
        registry.addInterceptor(loginAuthInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(Lists.newArrayList("/login/register"
                        ,"/login/info"
                        ,"/swagger-ui.html"
                        , "/swagger-resources/**"
                        ,"/swagger-ui.html/**"
                        ,"/swagger-resources/**"
                        ,"/v2/**"
                        ,"/webjars/**"
                ));
    }

    private void addCommonInterceptor(InterceptorRegistry registry) {
        registry.addInterceptor(commonInterceptor)
                .addPathPatterns("/**").excludePathPatterns(Lists.newArrayList("/swagger-ui.html"
                        , "/swagger-resources/**"
                        ,"/swagger-ui.html/**"
                        ,"/swagger-resources/**"
                        ,"/v2/**"
                        ,"/webjars/**"
                ));
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }

    /**
     * 交换MappingJackson2HttpMessageConverter与第一位元素
     * 让返回值类型为String的接口能正常返回包装结果
     *
     * @param converters initially an empty list of converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        for (int i = 0; i < converters.size(); i++) {
            if (converters.get(i) instanceof MappingJackson2HttpMessageConverter) {
                MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = (MappingJackson2HttpMessageConverter) converters.get(i);
                converters.set(i, converters.get(0));
                converters.set(0, mappingJackson2HttpMessageConverter);
                break;
            }
        }
    }
}
